package org.zn.mvcbase.mybatis.ddl;



import java.util.Map;

import org.apache.ibatis.type.JdbcType;
import org.zn.commons.collection.Maps;

public class SqlTypeMapping {

	private String dialect;

	private Map<String, Map<String, String>> typeMapping;

	public SqlTypeMapping(String dialect) {
		this.dialect = dialect;

		init();
	}

	private void init() {

		typeMapping = Maps.newMap();

		Map<String, String> mysql = Maps.newMap();
		Map<String, String> oracle = Maps.newMap();
		Map<String, String> sqlserver = Maps.newMap();

		typeMapping.put("mysql", mysql);
		typeMapping.put("oracle", oracle);
		typeMapping.put("sqlserver", sqlserver);

		mysql.put("java.lang.String", "varchar(255)");
		mysql.put("java.lang.Integer", "int");
		mysql.put("java.util.Date", "datetime");
		mysql.put("java.lang.Double", "double(8,2)");
		mysql.put("java.lang.Float", "float(8,2)");
		mysql.put("java.lang.Long", "bigint");
		mysql.put("java.lang.Boolean", "bit");

		mysql.put("int", "int");
		mysql.put("double", "double(8,2)");
		mysql.put("float", "float(8,2)");
		mysql.put("long", "bigint");
		mysql.put("boolean", "bit");
		
		mysql.put(JdbcType.DATE.toString(), "date");
		mysql.put(JdbcType.TIME.toString(), "datetime");
		mysql.put(JdbcType.TIMESTAMP.toString(), "timestamp");
		
	
		oracle.put("java.lang.String", "VARCHAR2(255)");
		oracle.put("java.lang.Integer", "INT");
		oracle.put("java.util.Date", "TIMESTAMP");
		oracle.put("java.lang.Double", "NUMBER(8,2)");
		oracle.put("java.lang.Float", "NUMBER(4,2)");
		oracle.put("java.lang.Long", "LONG");
		oracle.put("java.lang.Boolean", "NUMBER(1)");

		oracle.put("int", "INT");
		oracle.put("double", "NUMBER(8,2)");
		oracle.put("float", "NUMBER(4,2)");
		oracle.put("long", "LONG");
		oracle.put("boolean", "NUMBER(1)");
		
		oracle.put(JdbcType.DATE.toString(), "DATE");
		oracle.put(JdbcType.TIME.toString(), "TIMESTAMP");
		oracle.put(JdbcType.TIMESTAMP.toString(), "TIMESTAMP");
		
		
		
		

	}

	public String getSqlType(String javaType) {
		return typeMapping.get(dialect).get(javaType);
	}

	public String getSqlType(String javaType, int length, int precision) {

		String type = typeMapping.get(dialect).get(javaType);
		String lang = "";

		if (javaType.equals("java.lang.Double") || javaType.equals("java.lang.Float") || javaType.equals("double")
				|| javaType.equals("float")) {
			lang = length + "," + precision;
		} else if (javaType.equals("java.lang.Integer") || javaType.equals("java.lang.Long") || javaType.equals("int")
				|| javaType.equals("long")) {

			if (length == 255) {
				lang = "8";
			} else {

				lang = length + "";

			}

		} else {

			lang = length + "";

		}

		type = type.replaceAll("(?<=\\()[^\\)]+", lang);

		return type;

	}
	

	public String getDialect() {
		return dialect;
	}

	public void setDialect(String dialect) {
		this.dialect = dialect;
	}

	public static void main(String[] args) {

		SqlTypeMapping m=new SqlTypeMapping("mysql");
		
		String sss =m.getSqlType("java.lang.String", 32, 0);

		System.out.println(sss);

		
		System.out.println(JdbcType.DATE);
		
		
	}

}
